package com.esun.payment_api.controller;

import com.esun.common.api.CommonPage;
import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.InvTaskDetailBean;
import com.esun.payment_api.domain.InvFormTask;
import com.esun.payment_api.domain.SysDictContract;
import com.esun.payment_api.domain.SysDictProject;
import com.esun.payment_api.dto.PInvInsParam;
import com.esun.payment_api.service.InvFormTaskService;
import com.esun.payment_api.service.SysDictProjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 发票-任务号分摊接口
 * Create by benett on 2022-03-10
 * */
@RestController
@RequestMapping("/inv/task")
public class InvFormTaskController {

    @Resource
    InvFormTaskService invFormTaskService;

    @Resource
    SysDictProjectService sysDictProjectService;

    //此接口已弃用，发票-任务号分摊：新增
    @PostMapping("/insert")
    @ResponseBody
    public CommonResult insert(@RequestBody InvFormTask record){
        int count = invFormTaskService.insert(record);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("发票-任务号分摊：根据Id删除")
    @RequestMapping ("/deleteById")
    public CommonResult deleteById(Integer id) {
        try {
            int count = invFormTaskService.deleteById(id);
            if (count > 0) {
                return CommonResult.success(count);
            } else {
                return CommonResult.failed("删除失败");
            }
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    //此接口已弃用，发票-任务号分摊：删除,支持批量
    @PostMapping("/delete")
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Integer> ids){
        int count = invFormTaskService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("发票-任务号分摊：修改/保存")
    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(@RequestBody InvFormTask record){
        if (record == null) {
            return CommonResult.failed("请检查参数");
        }
        if (BlankUtils.isBlank(record.getId())) {
            return CommonResult.failed("任务号id不能为空");
        }
        if (BlankUtils.isBlank(record.getContId())) {
            record.setContId(null);
        }
        int count = invFormTaskService.update(record);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("发票-任务号分摊：根据id返回任务号分摊")
    @GetMapping("/getById")
    @ResponseBody
    public CommonResult getById(@RequestParam Integer id){
        if (BlankUtils.isBlank(id)){
            return CommonResult.failed("id不能为空");
        }
        InvTaskDetailBean taskDetail = invFormTaskService.getById(id);
        return CommonResult.success(taskDetail);
    }

    //此接口已弃用，发票-任务号分摊：根据发票id返回任务号分摊列表
    @GetMapping("/getListByInvId")
    @ResponseBody
    public CommonResult<List<InvFormTask>> getListByInvId(@RequestParam Integer invId){
        List<InvFormTask> invFormTaskList = invFormTaskService.getListByInvId(invId);
        return CommonResult.success(invFormTaskList);
    }

    @ApiOperation("发票-任务号分摊：新增多条")
    @RequestMapping("/insertMultiple")
    public CommonResult insertMultiple(@RequestBody PInvInsParam param) {
        try {
            System.out.println(" idStr: " + param.getIdStr().replaceAll(",", ":"));
            param.setIdStr(param.getIdStr().replaceAll(",", ":"));
            System.out.println("idStr="+param.getIdStr()+"|invId="+param.getInvId()+"|coId="+param.getCoId()+"|userId="+param.getUserId());
            invFormTaskService.insertMultiple(param);
            return CommonResult.success("新增任务号成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("发票-任务号分摊：根据发票id获取可选择任务号列表")
    @GetMapping("/getInvSelectTaskList")
    public CommonResult getInvSelectTaskList(Short coId, Integer invId, String year, String searchStr,
                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        if (BlankUtils.isBlank(coId)) {
            return CommonResult.failed("核算单位id不能为空");
        }
        if (BlankUtils.isBlank(invId)) {
            return CommonResult.failed("发票id不能为空");
        }
        List<SysDictProject> list = sysDictProjectService.getInvSelectTaskList(coId, invId, year, searchStr, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list), "获取可选任务号列表成功");
    }

    @ApiOperation("发票-任务号分摊：获取可关联合同列表")
    @GetMapping("/getSelectContractList")
    public CommonResult getSelectContractList(Short coId, Integer invId, String searchStr,
                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        if (BlankUtils.isBlank(coId)) {
            return CommonResult.failed("核算单位id不能为空");
        }
        if (BlankUtils.isBlank(invId)) {
            return CommonResult.failed("发票id不能为空");
        }
        List<SysDictContract> list = invFormTaskService.getSelectContractList(coId, invId, searchStr, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list), "获取可关联合同列表成功");
    }

}
